package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.HttpClientUtils;
import com.ewili.x.special.dao.inter.ThemeDao;
import com.ewili.x.special.entity.Theme;
import com.ewili.x.special.service.data.inter.ThemeService;
import com.ewili.x.special.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ThemeServiceImpl implements ThemeService {

    @Autowired
    private ThemeDao themeDao;

    @Override
    public void spideEastMoneyTheme() {
        themeDao.deleteAll();

        this.spideTodayOpportunityWeb();
        this.spideRecentHot();
        this.spidePrematureIncubation();

    }

    private void spideRecentHot() {

        String content = HttpClientUtils.getContent("http://quote.eastmoney.com/zhuti/api/recenthot", 300000, 300000);

        JSONObject object = JSONObject.parseObject(content);
        JSONArray objectArr = object.getJSONArray("result");
        JSONObject obj = objectArr.getJSONObject(0);
        JSONArray objArr = obj.getJSONArray("Data");
        String themeType = Constant.ThemeType.recentHot;
        for (int i = 0; i < objArr.size(); i++) {
            String value = objArr.getString(i);
            String[] lenArr = value.split("[|]", -1);

            String themeCode = lenArr[0] != null && !lenArr[0].isEmpty() && !lenArr[0].equals("") ? String.valueOf(lenArr[0]) : null;
            String themeName = lenArr[1] != null && !lenArr[1].isEmpty() && !lenArr[1].equals("") ? String.valueOf(lenArr[1]) : null;
            Integer isImportant = lenArr[2] != null && !lenArr[2].isEmpty() && !lenArr[2].equals("") ? Integer.valueOf(lenArr[2]) : null;
            Long parentCode = lenArr[3] != null && !lenArr[3].isEmpty() && !lenArr[3].equals("") ? Long.valueOf(lenArr[3]) : null;
            String parentName = lenArr[4] != null && !lenArr[4].isEmpty() && !lenArr[4].equals("") ? String.valueOf(lenArr[4]) : null;

            Theme theme = new Theme();
            theme.setThemeCode(themeCode);
            theme.setThemeName(themeName);
            theme.setIsImportant(isImportant);
            theme.setParentCode(parentCode);
            theme.setParentName(parentName);
            theme.setType(themeType);
            themeDao.save(theme);
        }


    }

    private void spidePrematureIncubation() {

        String content = HttpClientUtils.getContent("http://quote.eastmoney.com/zhuti/api/futurechance", 300000, 300000);

        JSONObject object = JSONObject.parseObject(content);
        JSONArray objectArr = object.getJSONArray("result");
        JSONObject obj = objectArr.getJSONObject(0);
        JSONArray objArr = obj.getJSONArray("Data");
        String themeType = Constant.ThemeType.prematureIncubation;
        for (int i = 0; i < objArr.size(); i++) {
            String value = objArr.getString(i);
            String[] lenArr = value.split("[|]", -1);

            String themeCode = lenArr[0] != null && !lenArr[0].isEmpty() && !lenArr[0].equals("") ? String.valueOf(lenArr[0]) : null;
            String themeName = lenArr[1] != null && !lenArr[1].isEmpty() && !lenArr[1].equals("") ? String.valueOf(lenArr[1]) : null;
            Integer isImportant = lenArr[6] != null && !lenArr[6].isEmpty() && !lenArr[6].equals("") ? Integer.valueOf(lenArr[6]) : null;
            Long parentCode = lenArr[2] != null && !lenArr[2].isEmpty() && !lenArr[2].equals("") ? Long.valueOf(lenArr[2]) : null;
            String parentName = lenArr[3] != null && !lenArr[3].isEmpty() && !lenArr[3].equals("") ? String.valueOf(lenArr[3]) : null;
            String reason = lenArr[5] != null && !lenArr[5].isEmpty() && !lenArr[5].equals("") ? String.valueOf(lenArr[5]) : null;
            String happenDate = lenArr[4] != null && !lenArr[4].isEmpty() && !lenArr[4].equals("") ? String.valueOf(lenArr[4]) : null;


            Theme theme = new Theme();
            theme.setThemeCode(themeCode);
            theme.setThemeName(themeName);
            theme.setIsImportant(isImportant);
            theme.setParentCode(parentCode);
            theme.setParentName(parentName);
            theme.setType(themeType);
            theme.setReason(reason);
            theme.setHappenDate(happenDate);
            themeDao.save(theme);
        }


    }


    private void spideTodayOpportunityWeb() {

        String content = HttpClientUtils.getContent("http://quote.eastmoney.com/zhuti/api/todayopportunity", 300000, 300000);

        JSONObject object = JSONObject.parseObject(content);
        JSONArray objectArr = object.getJSONArray("result");
        JSONObject obj = objectArr.getJSONObject(0);
        JSONArray objArr = obj.getJSONArray("Data");
        String themeType = Constant.ThemeType.todayOpportunityWeb;
        for (int i = 0; i < objArr.size(); i++) {
            String value = objArr.getString(i);
            String[] lenArr = value.split("[|]", -1);

            String themeCode = lenArr[1] != null && !lenArr[1].isEmpty() && !lenArr[1].equals("") ? String.valueOf(lenArr[1]) : null;
            String themeName = lenArr[2] != null && !lenArr[2].isEmpty() && !lenArr[2].equals("") ? String.valueOf(lenArr[2]) : null;
            Integer isImportant = lenArr[3] != null && !lenArr[3].isEmpty() && !lenArr[3].equals("") ? Integer.valueOf(lenArr[3]) : null;
            Long parentCode = lenArr[4] != null && !lenArr[4].isEmpty() && !lenArr[4].equals("") ? Long.valueOf(lenArr[4]) : null;
            String parentName = lenArr[5] != null && !lenArr[5].isEmpty() && !lenArr[5].equals("") ? String.valueOf(lenArr[5]) : null;
            String reason = lenArr[6] != null && !lenArr[6].isEmpty() && !lenArr[6].equals("") ? String.valueOf(lenArr[6]) : null;


            Theme theme = new Theme();
            theme.setThemeCode(themeCode);
            theme.setThemeName(themeName);
            theme.setIsImportant(isImportant);
            theme.setParentCode(parentCode);
            theme.setParentName(parentName);
            theme.setType(themeType);
            theme.setReason(reason);
            themeDao.save(theme);
        }


    }
}
